package igou.togogo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import igou.togogo.bean.EBrand;
import igou.togogo.bean.EBrandExample;
import igou.togogo.mapper.EBrandMapper;
import igou.togogo.pojo.EUDataGridResult;
import igou.togogo.pojo.EgouResult;
import igou.togogo.service.IBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    EBrandMapper brandMapper;

    @Override
    public EBrand getById(Integer id) {
        EBrandExample example = new EBrandExample();
        example.createCriteria().andIdEqualTo(id);
        List<EBrand> eBrands = brandMapper.selectByExample(example);
        /*查询判断*/
        if (eBrands.size() > 0){
            return eBrands.get(0);
        }
        return null;
    }

    /*新增品牌*/
    @Override
    public EgouResult addBrand(EBrand brand) {
        brandMapper.insert(brand);
        return EgouResult.ok();
    }

    @Override
    public EUDataGridResult getBrandList(Integer page, Integer rows) {
        EBrandExample example = new EBrandExample();
        //加载分页处理 page row
        PageHelper.startPage(page,rows);
        //加载数据
        List<EBrand> eBrandList = brandMapper.selectByExample(example);
        //创建一个dataGrid返回对象
        EUDataGridResult result = new EUDataGridResult();
        result.setRows(eBrandList);
        //取记录总数
        PageInfo<EBrand> pageInfo = new PageInfo<>(eBrandList);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return brandMapper.deleteByPrimaryKey(id);
    }


}
